package org.example.controller;

import org.example.vo.CacheVO;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")
public class IndexController {

    @GetMapping("/test1/{id}")
    @Cacheable(value = "abc", key = "'user_' + #id")
    public CacheVO test1(@PathVariable("id") String id) {
        CacheVO cacheVO = new CacheVO();
        cacheVO.setName("test1");
        cacheVO.setAge(11);
        CacheVO.User user = new CacheVO.User();
        user.setAge(22);
        user.setName("22");
        cacheVO.setUser(user);
        return cacheVO;
    }

    /**
     * CachePut
     * 每次都会将执行结果以键值对的形式存入指定的缓存中
     */
    @GetMapping("/test2/{id}")
    @CachePut(value = "abc", key = "#id")
    public CacheVO test2(@PathVariable("id") String id) {
        CacheVO cacheVO = new CacheVO();
        cacheVO.setName("test2");
        cacheVO.setAge(33);
        CacheVO.User user = new CacheVO.User();
        user.setAge(44);
        user.setName("44");
        cacheVO.setUser(user);
        return cacheVO;
    }
}
